package com.nnfe.fyd.feature.inquiry;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.nnfe.fyd.BusKey;
import com.nnfe.fyd.R;
import com.nnfe.fyd.ShareKey;
import com.nnfe.fyd.base.ActionBarFragment;
import com.nnfe.fyd.base.recycler.DividerItemDecoration;
import com.nnfe.fyd.bean.HttpResult;
import com.nnfe.fyd.bean.myinquirt.InquirtBean;
import com.nnfe.fyd.http.CallbackImpl;
import com.nnfe.fyd.utils.SharePrefUtil;
import com.nnfe.fyd.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by dada on 2017/3/21.
 */

public class MyInquiryFragment_recycler extends ActionBarFragment {

    @Bind(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;
    @Bind(R.id.recycler)
    RecyclerView recycler;

    private final int PAGE_SIZE = 10;

    private int PAGE = 0;
    private List listData = new ArrayList();
    private InquiryAdapter_recycler recyclerAdapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        donet(PAGE);
        EventBus.getDefault().register(this);
        initView();
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        EventBus.getDefault().unregister(this);
    }

    @Override
    public Object getTitleString() {
        return "我的询价";
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_recycle;
    }

    private void donet(final int page) {
        String id = SharePrefUtil.getString(mContext, ShareKey.UserId);
        service.inquiryList(page + "", "" + PAGE_SIZE, id).enqueue(new CallbackImpl<InquirtBean>() {
            @Override
            public void responseSuccess(Call<HttpResult<InquirtBean>> call, Response<HttpResult<InquirtBean>> response) {

                if (response.body() != null && response.body().getStatus() == 0) {
                    if (page == 0) {
                        listData.clear();
                    }
                    listData.addAll(response.body().getData().content);
                    if ((response.body().getData().content != null && response.body().getData().content.size() == 0)
                            || listData.size() < (page + 1) * PAGE_SIZE) {
                        ToastUtil.showToast("没有更多数据了...");
                    }
                    Log.e("xyd", "获取到数据个数：" + listData.size());
                    recyclerAdapter.notifyDataSetChanged();

                }
            }

            @Override
            protected void onRequestFinish() {
                super.onRequestFinish();
                swipeRefresh.setRefreshing(false);
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String str) {
        if (str.equalsIgnoreCase(BusKey.POST_inquiryReport)) {
            Log.e("xyd", str);
            PAGE = 0;
            donet(PAGE);
        }
    }

    @Override
    protected void initView() {

        recycler.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerAdapter = new InquiryAdapter_recycler(mContext,listData,R.layout.layout_inquiry1);
        recycler.setAdapter(recyclerAdapter);
        recycler.addItemDecoration(new DividerItemDecoration(mContext,DividerItemDecoration.VERTICAL_LIST));



        swipeRefresh.setEnabled(true);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                PAGE = 0;
                donet(PAGE);
            }
        });

    }


}
